package com.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.api.config.ApiConstant;
import com.api.entity.Domain;
import com.api.entity.Project;
import com.api.entity.User;
import com.api.service.DomainService;
import com.api.service.ProjectService;
import com.api.util.ApiJson;

/**
 * 
 * CreateDate:2017年9月28日下午1:16:56
 * 
 * @Description: 项目控制类
 * @author:zw
 * @version V1.0
 */
@Controller
@RequestMapping("/project")
public class ProjectController extends BaseController {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private DomainService domainService;

	@RequestMapping("/index")
	public String index() {
		// setAttr("project_id", getPara("project_id"));
		return "project/projectList";
	}

	@RequestMapping("/projectList")
	public String projectList() {
		return "project/projectList";
	}

	@RequestMapping("/getProjectLists")
	@ResponseBody
	public ApiJson getProjectLists(HttpServletRequest req,Project project, Model model) {

		ApiJson apiJson = new ApiJson();
		User user = (User) req.getSession().getAttribute(ApiConstant.LOGIN_SESSION_NAME);
		if (user!=null) {
			project.setCreateBy(user.getId());
		}
		List<Project> projectList = projectService.getProjectList(project);
		apiJson.setIsSuccess(true);
		apiJson.setCode("0");
		apiJson.setMessage("请求成功");
		apiJson.setData(projectList);

		return apiJson;
	}

	@RequestMapping("/delProject")
	@ResponseBody
	public ApiJson delProject(String projectId, Model model) {
		ApiJson apiJson = new ApiJson();
		apiJson.setMessage("删除成功");
		if (StringUtils.isNotEmpty(projectId)) {
			Project queryProject = projectService.getProject(projectId);
			if (queryProject != null) {
				queryProject.setStatus(0);
				int i = projectService.updateProject(queryProject);
				if (i <= 0) {
					apiJson.setMessage("删除失敗");
				}
			}
		}
		return apiJson;
	}

	@RequestMapping("/addProject")
	public String addProjectPage() {
		return "addProject.html";
	}

	@RequestMapping("/saveOrEditProject")
	public String saveOrEditProject(String projectId, Model model) {
		if (StringUtils.isNotEmpty(projectId)) {
			Project project = projectService.getProjectById(projectId);
			model.addAttribute("project", project);
		} else {
			model.addAttribute("project", new Project());
		}
		return "project/saveOrEdit";
	}

	@RequestMapping("/doSaveOrEdit")
	@ResponseBody
	public ApiJson doSaveOrEdit(HttpServletRequest req,Project project) {

		ApiJson apiJson = new ApiJson();
		apiJson.setMessage("操作成功");
		int i = 0;
		if (StringUtils.isNotEmpty(project.getId())) {
			i = projectService.updateProject(project);
		} else {
			User user = (User) req.getSession().getAttribute(ApiConstant.LOGIN_SESSION_NAME);
			if (user==null) {
				apiJson.setMessage("请重新登录");
				return apiJson;
			}
			project.setCreateBy(user.getId());
			project.setStatus(1);
			i = projectService.add(project);
		}
		if (i <= 0) {
			apiJson.setMessage("操作失敗");
		}
		return apiJson;
	}

	@RequestMapping("/saveDomain")
	public String saveDomain(Model model, String project_id) {
		model.addAttribute("project_id", project_id);
		return "project/saveDomain";
	}

	@RequestMapping("/selectDelDomain")
	public String selectDelDomain(Model model, String project_id) {
		model.addAttribute("project_id", "project_id");
		List<Domain> domainList = domainService.getDomainList(project_id);
		model.addAttribute("domainList", domainList);
		return "project/selectDelDomain";
	}

	@RequestMapping("/doSaveDomain")
	@ResponseBody
	public ApiJson doSaveDomain(Domain domain) {
		ApiJson apiJson = new ApiJson();
		int i = domainService.add(domain);
		if (i > 0) {
			apiJson.setCode("0");
			apiJson.setMessage("请求成功");
		} else {
			apiJson.setCode("-2");
			apiJson.setMessage("请求失败");
		}
		return apiJson;
	}

	@RequestMapping("/delDomain")
	@ResponseBody
	public ApiJson delDomain(String domainId) {
		ApiJson apiJson = new ApiJson();
		Domain domain = domainService.getDomainById(domainId);
		int i = 0;
		if (domain != null) {
			i = domainService.deleteById(domainId);
		}
		if (i > 0) {
			apiJson.setCode("0");
			apiJson.setMessage("请求成功");
		} else {
			apiJson.setCode("-2");
			apiJson.setMessage("请求失败");
		}
		return apiJson;
	}
}
